package com.mykt.util;

import com.mykt.constant.SecurityConstants;
import com.mykt.constant.TokenConstants;
import com.mykt.utils.StringUtils;
import com.mykt.utils.servlet.ServletUtils;
import javax.servlet.http.HttpServletRequest;


/**
 * 当前登录用户信息获取
 */
public class CurrentUserSecurityUtils {

    /**
     * request中获取token
     * @return
     */
    public static String getToken() {
        HttpServletRequest request = ServletUtils.getRequest();
        String token = request.getHeader(TokenConstants.AUTHENTICATION);
        // 如果前端设置了令牌前缀，则裁剪掉前缀
        if (StringUtils.isNotEmpty(token) && token.startsWith(TokenConstants.PREFIX)) {
            token = token.replaceFirst(TokenConstants.PREFIX, "");
        }
        return token;
    }


    /**
     * request head中 获取当前用户id
     * @return
     */
    public static String getUserId() {
        return ServletUtils.getHeaderParameter(SecurityConstants.DETAILS_USER_ID);
    }


    /**
     * head中 获取当前用户name
     * @return
     */
    public static String getUserName() {
        return ServletUtils.getHeaderParameter(SecurityConstants.DETAILS_USERNAME);
    }


}
